<?php
/**
 * ============================================================================
 * @package Forum
 * @author Andy
 * @version 11:08
 * @link https://sjdphp.com
 * ----------------------------------------------------------------------------
 */

namespace app\admin\controller;


use app\common\controller\BackBaseController;
use app\common\model\Plate as PlateModel;
use app\common\server\Json;
use app\common\server\Tree;
use app\common\model\Boroughs as BoroughsModel;
use app\common\server\SystemLog;
class Inv extends BackBaseController
{
    protected $model_id = 'inv_id';
    protected $model_name = 'inv_title';
    protected $model_cate = 'plate_id';
    protected $model_status = 'inv_status';
    protected $model_desc = 'view_num desc';


    public function lists(){
        ## 收参
        $page = $this->request->param("page",0,'intval');
        $limit = $this->request->param("limit",0,'intval');
        $searchParams = json_decode($this->request->param("searchParams"),true);

        ## 默认查询数据的状态
        $term[] = [$this->model_status,'<>',config("project.mysql_code.table_delete")];

        ## 判断是否存在搜索数据
        if(!empty($searchParams)){
            if(isset($searchParams['name']) && $searchParams['name'] !=''){
                $term[] = [$this->model_name|'inv_title','like','%'.$searchParams['name'].'%'];
            }
            if(isset($searchParams["cate_id"]) && is_numeric($searchParams['cate_id'])){
                $term[] = [$this->model_cate,'=',$searchParams['cate_id']];
            }
            if(isset($searchParams["bor_id"]) && is_numeric($searchParams['bor_id'])){
                $term[] = ['boroughs_id','=',$searchParams['bor_id']];
            }
        }

        ## 获取实例
        $class = self::getBusExample($this->request->controller());
        $res = $class::getLists($term,'',$page,$limit,$this->model_desc);

        ## 判断请求方式并返回数据
        if($this->request->isPost()){
            return $res;
        }

        ## 获取板块数据
        $plateLists = PlateModel::where(array("plate_status" => config("project.mysql_code.table_normal")))->field("plate_id,plate_name,pid")->select();
        ## 获取省份数据

        return view('',[
            'plates' => empty($plateLists) ? array() : Tree::getPlateTree($plateLists->toArray()),
            'bors'   => $this->getBoroughsLists()
        ]);
    }


    public function add(){
        if($this->request->isPost()){
            ## 接受参数
            $param = $this->request->param();

            ## 获取验证层实例对象
            $validate = self::getBusExample($this->request->controller(),2);
            if(!$validate->scene("add")->check($param)) return Json::error($validate->getError());

            ## 获取默认的状态
            $param[$this->model_status] = config("project.mysql_code.table_pending");
            ## 获取操作人
            $param["operator"] = $this->request->BackInfo["back_name"];

            ## 调用业务层处理
            $class = self::getBusExample($this->request->controller());
            $res = $class::insertOrUpdateAndDel($param);

            return $res;
        }else{
            ## 获取板块数据
            $plateLists = PlateModel::where(array("plate_status" => config("project.mysql_code.table_normal")))->field("plate_id,plate_name,pid,plate_type")->select();
            return view('',[
                'plates' => empty($plateLists) ? array() : Tree::getPlateTree($plateLists->toArray()),
                'bors'   => $this->getBoroughsLists()
            ]);
        }
    }


    public function edit(){
        if($this->request->isPost()){
            ## 接受参数
            $param = $this->request->param();

            ## 获取验证层实例对象
            $validate = self::getBusExample($this->request->controller(),2);
            if(!$validate->scene("edit")->check($param)) return Json::error($validate->getError());

            ## 获取操作人
            $param["operator"] = $this->request->BackInfo["back_name"];

            ## 调用业务层处理
            $class = self::getBusExample($this->request->controller());
            $res = $class::insertOrUpdateAndDel($param,2);

            return $res;
        }else{
            $inv_id = $this->request->param("inv_id",0,'intval');
            if(!empty($inv_id)){
                $class = self::getBusExample($this->request->controller());
                $info = $class::getInfo(array($this->model_id => $inv_id));
            }
            ## 获取板块数据
            $plateLists = PlateModel::where(array("plate_status" => config("project.mysql_code.table_normal")))->field("plate_id,plate_name,pid,plate_type")->select();
            return view('',[
                'info'  => $info,
                'plates' => empty($plateLists) ? array() : Tree::getPlateTree($plateLists->toArray()),
                'bors'   => $this->getBoroughsLists()
            ]);
        }
    }


    /** 获取行政列表
     * @return array
     */
    private function getBoroughsLists(){
        try{
            $lists = BoroughsModel::where(array("boroughs_status" => config("project.mysql_code.table_normal")))->field("boroughs_id,boroughs_name,pid")->select()->toArray();
        }catch(\Exception $e){
            SystemLog::insertLog($e->getMessage());
        }

        return Tree::getBoroughsLists($lists);
    }
}